package com.snpic.appaw.module.insure.controller.app.carmessage;

import com.snpic.appaw.module.insure.enums.common.BooleanEnum;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import com.snpic.appaw.framework.common.pojo.PageParam;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;
import static com.snpic.appaw.framework.common.pojo.CommonResult.success;
import com.snpic.appaw.framework.excel.core.util.ExcelUtils;
import com.snpic.appaw.module.insure.controller.app.carmessage.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.carmessage.CarMessageDO;
import com.snpic.appaw.module.insure.service.carmessage.CarMessageService;

@Tag(name = "C端 - 车险首页 - 车辆信息")
@RestController
@RequestMapping("/insure/car-message")
@Validated
public class AppCarMessageController {

	@Resource
	private CarMessageService carMessageService;

	@PostMapping("/create")
	@Operation(summary = "创建车辆信息")
	public CommonResult<AppCarMessageSaveRespVO> createCarMessage(@Valid @RequestBody AppCarMessageSaveReqVO createReqVO) {
		//如果id不为空，说明是更新操作TODO 是否每次都是新建还是允许更新?
		if(createReqVO.getId() !=null){
			carMessageService.updateCarMessage(createReqVO);
			return success(createReqVO.getId());
		}
		return success(carMessageService.createCarMessage(createReqVO));
	}

	@PostMapping("/update")
	@Operation(summary = "更新车辆信息")
	public CommonResult<Boolean> updateCarMessage(@Valid @RequestBody AppCarMessageSaveReqVO updateReqVO) {
		carMessageService.updateCarMessage(updateReqVO);
		return success(true);
	}

	@GetMapping("/delete")
	@Operation(summary = "删除车辆信息")
	@Parameter(name = "id", description = "ID", required = true)
	public CommonResult<Boolean> deleteCarMessage(@RequestParam("id") Long id) {
		carMessageService.deleteCarMessage(id);
		return success(true);
	}

	//    @PostMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除车险出单-车辆信息")
	public CommonResult<Boolean> deleteCarMessageList(@RequestParam("ids") List<Long> ids) {
		carMessageService.deleteCarMessageListByIds(ids);
		return success(true);
	}

//	@GetMapping("/get")
//	@Operation(summary = "获得车辆信息")
//	@Parameter(name = "id", description = "编号")
	public CommonResult<AppCarMessageRespVO> getCarMessage(@RequestParam("id") Long id) {
		CarMessageDO carMessage = carMessageService.getCarMessage(id);
		return success(BeanUtils.toBean(carMessage, AppCarMessageRespVO.class));
	}

	//    @GetMapping("/page")
//    @Operation(summary = "获得车险出单-车辆信息分页")
	public CommonResult<PageResult<AppCarMessageRespVO>> getCarMessagePage(@Valid AppCarMessagePageReqVO pageReqVO) {
		PageResult<CarMessageDO> pageResult = carMessageService.getCarMessagePage(pageReqVO);
		return success(BeanUtils.toBean(pageResult, AppCarMessageRespVO.class));
	}

	@GetMapping("/list")
	@Operation(summary = "获得车辆信息列表")
	public CommonResult<List<AppCarMessageRespVO>> getCarMessageList(@Valid AppCarMessagePageReqVO pageReqVO) {
		List<CarMessageDO> listResult = carMessageService.getCarMessageList(pageReqVO);
		List<AppCarMessageRespVO> appCarMessageRespVOList = BeanUtils.toBean(listResult, AppCarMessageRespVO.class);
		for(int i = 0; i < listResult.size();i++){
			appCarMessageRespVOList.get(i).setTransferFlag(BooleanEnum.fromValue(listResult.get(i).getTransferFlag()).toBoolean());
		}
		return success(appCarMessageRespVOList);
	}

	//    @GetMapping("/export-excel")
//    @Operation(summary = "导出车险出单-车辆信息 Excel")
//    @ApiAccessLog(operateType = EXPORT)
	public void exportCarMessageExcel(@Valid AppCarMessagePageReqVO pageReqVO,
																		HttpServletResponse response) throws IOException {
		pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
		List<CarMessageDO> list = carMessageService.getCarMessagePage(pageReqVO).getList();
		// 导出 Excel
		ExcelUtils.write(response, "车险出单-车辆信息.xls", "数据", AppCarMessageRespVO.class,
			BeanUtils.toBean(list, AppCarMessageRespVO.class));
	}

}